<template>
  <el-dialog title="选择处理单位" :visible.sync="visible" width="800px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="处理单位名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入处理单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="dataList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="处理单位名称" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="详情地址" prop="address" :show-overflow-tooltip="true" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectFactory">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { allocatedList,edit } from "@/api/system/dataScope";
export default {
  name: "selectFactory",
  props: {
    // 角色编号
    roleId: {
      type: [Number, String]
    },
    userId: {
      type: [Number, String]
    },
    queryParamsSelect: {
      type: Object
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      factoryIds: [],
      // 总条数
      total: 0,
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      // 已授权的数据id
      allocatedIds: []
    }
  },
  methods: {
    /** 显示弹框 */
    show() {
      this.getAllocatedList().then(() => {
        this.getList();
        this.visible = true;
      });
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    getList() {
      listFactory(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        // 获取完数据调用
        this.$nextTick(function(){
          if (this.allocatedIds.length > 0) {
            for (let i = 0; i < this.$refs.table.data.length; i++) {
              const item = this.$refs.table.data[i];
              if (this.allocatedIds.indexOf(item.id) !== -1) {
                this.$refs.table.toggleRowSelection(this.$refs.table.data[i],true)
              }
            }
          }
        })
      });
    },
    /** 获取已授权的处理单位列表 */
    getAllocatedList() {
      return new Promise((resolve, reject) => {
        allocatedList(this.queryParamsSelect,).then(response => {
          const arr = response.data || [];
          this.allocatedIds = arr.map(item => item.factoryId);
          resolve()
        }).catch(() => {
          reject()
        });
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      for (const item of selection) {
        console.log(item);
      }
      this.factoryIds = selection.map(item => item.id);
    },
    handleSelectFactory() {
      const form = {
        ...this.queryParamsSelect,
        factoryIds: this.factoryIds
      }
      this.$modal.confirm('是否确认该操作？').then(() => {
        edit(form).then(response => {
          this.$modal.msgSuccess("操作成功");
          this.getList();
          if (response.code === 200) {
            this.factoryIds = []
            this.visible = false;
            this.$emit("ok");
          }
        });
      })
    },
    cancel() {
      this.allocatedIds = []
      this.visible = false
    }
  }
}
</script>

<style scoped>

</style>
